﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;

public partial class SystemManager_GiftCardManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetGVData(null);
        }
    }
    protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvUserList.PageIndex = e.NewPageIndex;
        GetGVData();
    }
    protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void cbAll_CheckedChanged(object sender, EventArgs e)
    {
        // 遍历数据集的所有数据项
        foreach (GridViewRow row in this.gvUserList.Rows)
        {
            // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
            CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
            subCB.Checked = ((CheckBox)sender).Checked;
        }
    }
    
    private void GetBeSended()
    {
        GoShopping.BLL.SysInfo.GiftCardBean gcb = new GoShopping.BLL.SysInfo.GiftCardBean();
        XmlNodeList xnl = XMLManipulation.GetNodes(Server.MapPath("SysConfig.xml"), "//SysConfig/GiftCard/GiftMoney");
        GoShopping.Model.GiftCard gc;
        // 遍历数据集
        foreach (GridViewRow row in this.gvUserList.Rows)
        {
            // 获取行首的CheckBox，判断是否为Checked
            CheckBox cb = (CheckBox)row.FindControl("cbSingle");
            if (cb.Checked == false)
            {
                continue;
            }
            gc = new GoShopping.Model.GiftCard();
            gc.Enable = true;
            gc.CreateDate = DateTime.Now;
            if (xnl.Count != 0)
            {
                gc.CardPrice = Convert.ToDecimal(xnl[0].Attributes["Price"].Value);
            }
            else
            {
                gc.CardPrice = 0;
            }
            gc.BoundUserID = Convert.ToInt32(((Label)row.FindControl("lblID")).Text);
            gc.BoundUserName = ((Label)row.FindControl("lblName")).Text;
            gc.IsBound2User = true;
            try
            {
                gcb.CreateOneGiftCard(gc);
            }
            catch
            {
 
            }
        }
        //GetLVData(0);
    }

    private void GetLVData(int id)
    {
        GoShopping.BLL.SysInfo.GiftCardBean gcb = new GoShopping.BLL.SysInfo.GiftCardBean();
        lvSendHistory.DataSource = gcb.GetGiftCardContent(id);
        lvSendHistory.DataBind();
    }

    private void GetGVData()
    {
        
    }
    protected void lvSendHistory_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        ((DataPager)(this.lvSendHistory.FindControl("DataPager1"))).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        GetLVData(0);
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        GetBeSended();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetGVData(txtUser.Text.Trim());
    }
    GoShopping.BLL.UserBean ub = new GoShopping.BLL.UserBean();
    GoShopping.BLL.MemberShipService ms = new GoShopping.BLL.MemberShipService();
    private void GetGVData(string filterString)
    {
        IList<GoShopping.Model.UserInfo> iUser;
        if (string.IsNullOrEmpty(filterString))
        {
            iUser = ub.GetAllUserList();
        }
        else
        {
            iUser = ub.GetUserList(" AND Name LIKE '%" + filterString + "%'");

        }
        this.gvUserList.DataSource = iUser;
        this.gvUserList.DataBind();
    }
}
